package com.zy.core.util.page;

import com.zy.core.util.json.ComplexPropertyPreFilter;
import com.zy.core.util.json.FastJsonUtils;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 *
 * Package: core.util.page
 * <p>
 * Description： TODO
 * <p>
 * Author: zhangyu
 * <p>
 * Date: Created in 2018-10-5 21:50
 * <p>
 * Company: mingtu
 * <p>
 * Copyright: Copyright (c) 2018
 * <p>
 * Version: 0.0.1
 * <p>
 * Modified By:
 */
public class Result<T extends Object> implements Serializable {
    /**
     * 返回状态
     * @ClassName ErrorCode
     * @Description TODO
     * @author huangshun
     * @date 2015年10月27日
     */
    public enum ErrorCode{
        suc(0,"ok"),//调用成功
        auth(1,"auth"),//认证失败
        fail(2,"fail"),//调用失败
        err(-1,"err");//调用错误

        private int code;

        private String msg;

        ErrorCode(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    /**
     * 主体内容
     */
    private T body;
    /**
     * 返回信息
     */
    private ErrorCode errorCode;
    /**
     * 消息信息
     */
    private String msg;

    public Result(){

    }

    public Result(T body){
        this.body=body;
    }

    public void suc(){
        this.errorCode=ErrorCode.suc;
    }

    public void suc(String msg){
        this.errorCode=ErrorCode.suc;
        this.msg=msg;
    }

    public void fail(String msg){
        this.errorCode=ErrorCode.fail;
        this.msg=msg;
    }

    public void error(String msg){
        this.errorCode=ErrorCode.err;
        this.msg=msg;
    }

    public void errorCode(ErrorCode errorCode) {
        this.errorCode = errorCode;
    }


    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setBody(T body){
        this.body=body;
    }

    public T getBody() {
        return body;
    }

    public ErrorCode getErrorCode() {
        return errorCode;
    }

    public String getMsg() {
        return msg;
    }

    public String readerJson(){

        return readerJson(null);
    }

    public String readerJson(ComplexPropertyPreFilter filter){

        return FastJsonUtils.toJSONString(this,filter);
    }
}
